package fr.kraml;

import java.util.ArrayList;
import java.util.Iterator;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.*;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class ParticipantFragment extends Fragment {

	ListView lv;
	View mView;
	ParticipantFragmentListener mCallback;
	MySimpleArrayAdapter adapter;
	ArrayList<Participant> participants = new ArrayList<Participant>();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);

		// This makes sure that the container activity has implemented
		// the callback interface. If not, it throws an exception
		try {
			mCallback = (ParticipantFragmentListener) activity;
		} catch (ClassCastException e) {
			throw new ClassCastException(activity.toString()
					+ " must implement LoginFragmentListener");
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// Inflate the layout for this fragment
		mView = inflater.inflate(R.layout.participant_fragment, container,
				false);

		lv = (ListView) mView.findViewById(R.id.participantListView);
		adapter = new MySimpleArrayAdapter(this.getActivity(), participants);
		lv.setAdapter(adapter);

		lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
			@Override
			public void onItemClick(AdapterView<?> parent, final View view,
					int position, long id) {
				/*
				 * final String item = (String)
				 * parent.getItemAtPosition(position);
				 * view.animate().setDuration(2000).alpha(0) .withEndAction(new
				 * Runnable() {
				 * 
				 * @Override public void run() { list.remove(item);
				 * adapter.notifyDataSetChanged(); view.setAlpha(1); } });
				 */
			}

		});
		
		mCallback.loadBDDParticipants();

		return mView;

	}

	public ParticipantFragment() {
		// TODO Auto-generated constructor stub
	}

	// Container Activity must implement this interface
	public interface ParticipantFragmentListener {

		void loadBDDParticipants();

		/*void add_asv_cache(String from, int age, String sex,
				String location);*/
		// public void onGroupChatSend(String text);
	}

	class MySimpleArrayAdapter extends ArrayAdapter<Participant> {
		private final Context context;
		private final ArrayList<Participant> participants;

		public MySimpleArrayAdapter(Context context,
				ArrayList<Participant> participants) {
			super(context, R.layout.participant_fragment_item, participants);
			this.context = context;
			this.participants = participants;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			View rowView = inflater.inflate(R.layout.participant_fragment_item,
					parent, false);
			TextView textView = (TextView) rowView
					.findViewById(R.id.participant_item_text);
			ImageView imageView = (ImageView) rowView
					.findViewById(R.id.participant_item_image);
			textView.setText(participants.get(position).username + "\n"
					+ participants.get(position).getASV());
			//imageView.setImageBitmap(participants.get(position).avatarBMP);
			imageView.setImageDrawable(participants.get(position).avatarBMP);

			return rowView;
		}
	}

	public boolean isParticipant(String username) {
		Iterator<Participant> i = participants.iterator();
		while (i.hasNext()) {
			Participant p = (Participant) i.next();
			if (p.username.equals(username))
				return true;
		}
		return false;
	}

	public Participant getParticipant(String username) {
		Iterator<Participant> i = participants.iterator();
		while (i.hasNext()) {
			Participant p = (Participant) i.next();
			if (p.username.equals(username))
				return p;
		}
		return null;
	}

	public void changeParticipant(String pres_from, String show, String status,
			int age, String sex, String location, String avatar) {

		if (show.equals("chat")) {
			if (getParticipant(pres_from) == null) {
				Participant p = new Participant(this, pres_from, show, status,
						age,
						sex,
						location,
						avatar);
				participants.add(p);
			} else {
				Participant p = getParticipant(pres_from);
				p.show = show;
				p.status = status;
				p.age = age;
				p.sex = sex;
				p.location = location;				
			}
		} else if (show.equals("unavailable")) {
			if (isParticipant(pres_from)) {
				Participant p = getParticipant(pres_from);
				participants.remove(p);
			}
		}
		adapter.notifyDataSetChanged();
	}

	void reload() {
		// TODO Auto-generated method stub
		adapter = new MySimpleArrayAdapter(getActivity(), participants);
		lv.setAdapter(adapter);
	}

}
